﻿using MyControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ModbusLib;
using Models;
using System.IO.Ports;
using DAL;
using static DAL.IniConfigHelper;

namespace 温湿度传感器
{
    public partial class FrmParamSet : Form
    {
        private string fileName = Application.StartupPath + "\\config\\config.ini";

        public FrmParamSet()
        {
            InitializeComponent();

            //设置ini文件路径
            IniConfigHelper.filePath = fileName;

            InitInfo();

        }

        public FrmParamSet(Config config):this()
        {
            cboPortName.Text = config.PortName;
            cboBaud.Text = config.BaudRate.ToString();
            cboParity.Text = config.Parity.ToString();
            cboDataBit.Text = config.DataBits.ToString();
            cboStopBit.Text = config.StopBits.ToString();
            nudNode1.Text = config.SlaveId1.ToString();
            nudNode2.Text = config.SlaveId2.ToString();
            nudNode3.Text = config.SlaveId3.ToString();
            nudNode4.Text = config.SlaveId4.ToString();
        }

        private void InitInfo()
        {
            cboPortName.DataSource = SerialPort.GetPortNames();
            cboBaud.DataSource =new string[] { "4800","9600","19200" };
            cboParity.DataSource = Enum.GetNames(typeof(Parity));
            cboDataBit.DataSource = new string[] { "7", "8" };
            cboStopBit.DataSource = Enum.GetNames(typeof(StopBits));
        }



        #region 窗体移动

        Point mPoint;


        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                Location = new Point(Location.X + e.X - mPoint.X, Location.Y + e.Y - mPoint.Y);
            }
        }


        #endregion

        private void lblClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (SaveParam())
            {
                MessageBox.Show("参数保存成功");
                Close();
            }
            else
            {
                MessageBox.Show("参数保存失败");
            }
        }

        private bool SaveParam()
        {
            var res = true;
            res &= WriteIniData("串口参数", "端口号", cboPortName.Text);
            res &= WriteIniData("串口参数", "波特率", cboBaud.Text);
            res &= WriteIniData("串口参数", "校验位", cboParity.Text);
            res &= WriteIniData("串口参数", "数据位", cboDataBit.Text);
            res &= WriteIniData("串口参数", "停止位", cboStopBit.Text);

            res &= WriteIniData("站号参数", "站1", nudNode1.Text);
            res &= WriteIniData("站号参数", "站2", nudNode2.Text);
            res &= WriteIniData("站号参数", "站3", nudNode3.Text);
            res &= WriteIniData("站号参数", "站4", nudNode4.Text);

            return res;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
